<?php
/**
 * 处理文本回复信息
 */

class HandleTextAction extends Action{

	public $postObj;
	public $keyword;
	public $openid;
	public function __construct($postObj){

		parent::__construct();

        $keyword = trim($postObj->Content); //去除头尾空格
        $keyword = strtolower($keyword);  	//转化为小写

        $this->postObj = $postObj;
 		$this->keyword = $keyword;
 		$this->openid  = $postObj->FromUserName.'';

        $this->analyse();			//处理关键词回复
        // 无法匹配 返回默认设置
        $default = '无法匹配';
        returnText($this->postObj,$default.' '.$this->keyword);
	}

	public function index(){
		echo 'No Access';
	}


	/**
	 * [处理关键词回复]
	 * @return [type]          [description]
	 */
	private function analyse(){
		// 系统命令匹配
		$this->assign_command();

		// 固定回复
		$this->fixedReply();

		// 匹配用户配置
		// $content = M('return_by_keywords')->where("keyword LIKE '%".$this->keyword."%'")->getField('content');
		// if(!empty($content)){
		// 	returnText($this->postObj,$content);
		// }
	}

	/**
	 * 指定的命令匹配
	 * @return [type]          [description]
	 */
	private function assign_command(){

		switch ($this->keyword) {
			case 'help':
				$this->help();
				break;
			case '帮助':
				$this->help();
				break;
			case '订单查询':
				$this->order_query();
				break;
			case '兑换':
				$domain = 'http://single.wzxing.com';
				returnText($this->postObj,$domain.U('Weixin/Index/index',array('openid'=>$this->openid)),'',true);
				break;
			default:
				# code...
				break;
		}

	}

	/**
	 * 帮助提示
	 * @return [type] [description]
	 */
	private function help(){
		returnText($this->postObj,'help');
	}


	/**
	 * 固定回复
	 */
	private function fixedReply(){
		$val = M('wx_answer')->where(" _key = '{$this->keyword}'")->getField('_value');
		// returnText($this->postObj,M('wx_answer')->getLastSql());
		if($val){
			returnText($this->postObj,$val);
		}
	}
}
